package com.openx.xoj.controller;
import com.openx.xoj.model.entity.Question;
import com.openx.xoj.service.QuestionBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/questions/get/vo")
public class QuestionBasicController {

    private final QuestionBasicService questionsService;

    @Autowired
    public QuestionBasicController(QuestionBasicService questionsService) {
        this.questionsService = questionsService;
    }

    @PostMapping("/ams")
    public ResponseEntity<List<Question>> getQuestionsByTopic(@RequestBody Map<String, String> requestData) {
        String am = requestData.get("am");
        List<Question> questions = questionsService.findAll();

        // 筛选出questions 中tags包含am的题目
        questions = questions.stream()
                .filter(question -> question.getTags().contains(am))
                .collect(Collectors.toList());

        return ResponseEntity.ok(questions);
    }

    @GetMapping("/all")
    public ResponseEntity<List<Question>> getAllQuestions() {
        List<Question> questions = questionsService.findAll();
        return ResponseEntity.ok(questions);
    }


    @GetMapping("/Array")
    public ResponseEntity<List<Question>> getArrayQuestions() {
        List<Question> questions = questionsService.findAll();

        // 筛选出questions 中tags包含"字符串"的题目
        questions = questions.stream()
                .filter(question -> question.getTags().contains("数组"))
                .toList();

        return ResponseEntity.ok(questions);
    }

    @GetMapping("/Sort")
    public ResponseEntity<List<Question>> getSortQuestions() {
        List<Question> questions = questionsService.findAll();

        // 筛选出questions 中tags包含"字符串"的题目
        questions = questions.stream()
                .filter(question -> question.getTags().contains("排序"))
                .toList();

        return ResponseEntity.ok(questions);
    }

    @GetMapping("/enumerate")
    public ResponseEntity<List<Question>> getEnumerateQuestions() {
        List<Question> questions = questionsService.findAll();

        // 筛选出questions 中tags包含"字符串"的题目
        questions = questions.stream()
                .filter(question -> question.getTags().contains("枚举"))
                .toList();

        return ResponseEntity.ok(questions);
    }
}
